package com.yanqu.road.dao.impl.activity.festivagift;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.festivalgift.FestivalGiftData;
import com.yanqu.road.entity.activity.fivehero.FiveHeroUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class FestivalGiftDaoImpl extends TempDataObjectDao<FestivalGiftData> {

    public static String getUserMallSql = "select * from t_u_activity_festival_gift_data where activityId=? and userId=?";

    public Map<Integer, FestivalGiftData> getUserMall(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, FestivalGiftData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserMallSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                FestivalGiftData data = parse(rs);
                map.put(data.getMallId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertSql = "insert into t_u_activity_festival_gift_data(" +
            "activityId,userId,mallId,prop,buyNum,propIndexList) " +
            " values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(FestivalGiftData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMallId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getProp()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBuyNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getPropIndexList(), ",")));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "update t_u_activity_festival_gift_data " +
            "set prop=?,buyNum=?,propIndexList=?" +
            " where activityId=? and userId=? and mallId=?";
    @Override
    protected boolean updateData(FestivalGiftData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, data.getProp()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBuyNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getPropIndexList(), ",")));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMallId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected FestivalGiftData parseData(ResultSet rs) throws SQLException {
        FestivalGiftData data = new FestivalGiftData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setMallId(rs.getInt("mallId"));
        data.setProp(rs.getString("prop"));
        data.setBuyNum(rs.getInt("buyNum"));
        if (!StringUtils.isNullOrEmpty(rs.getString("propIndexList"))) {
            data.setPropIndexList(StringUtils.stringToIntegerList(rs.getString("propIndexList"), ","));
        }
        return data;
    }
}
